﻿using System;
using System.Web.UI;
using System.IO;
using Presentation;
using Infrastructure;

namespace WebClient
{
    public partial class LeftSideHeapWebForm : Page, IView
    {
        private Presenter presenter;

        public LeftSideHeapWebForm()
        {
            var root = Server.MapPath("~");
            Directory.SetCurrentDirectory(root);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["presenter"] == null)
            {
                var logger = new TxtLogger();
                presenter = new Presenter(this, logger);
                Session["logger"] = logger;
                Session["presenter"] = presenter;
            }
            else
            {
                presenter = (Presenter) Session["presenter"];
                presenter.setView(this);
            }
        }

        public string Key
        {
            get
            {
                return (string) Session["key"];
            }
            set
            {
                Session["key"] = value;           
            }
        }

        public string Value
        {
            get
            {
                return (string)Session["value"];
            }
            set
            {
                Session["value"] = value;
            }
        }

        public string Status
        {
            get
            {
                return (string)Session["status"];
            }
            set
            {
                Session["status"] = value;
            }
        }

        public string MinValue
        {
            get
            {
                return (string)Session["minValue"];
            }
            set
            {
                Session["minValue"] = value;
            }
        }

        public string Log
        {
            get
            {
                return (string)Session["log"];
            }
            set
            {
                Session["log"] = value;
            }
        }

        public event Action OnInsert;
        public event Action OnGetMin;
        public event Action OnDeleteMin;
        public event Action OnDataChanged;

        protected void ButtonInsert_Click(object sender, EventArgs e)
        {
            Key = TextBoxKey.Text;
            Value = TextBoxValue.Text;
            OnInsert();
            LabelStatus.Text = Status;
            TextBoxLog.Text = Log;
        }

        protected void ButtonGetMin_Click(object sender, EventArgs e)
        {
            OnGetMin();
            TextBoxMin.Text = MinValue;
            LabelStatus.Text = Status;
            TextBoxLog.Text = Log;
        }

        protected void ButtonDeleteMin_Click(object sender, EventArgs e)
        {
            OnDeleteMin();
            TextBoxMin.Text = MinValue;
            LabelStatus.Text = Status;
            TextBoxLog.Text = Log;
        }

        protected void RadioGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (RadioGroup.SelectedIndex)
            {
                case 0:
                    presenter.setLogger(new TxtLogger());
                    break;
                case 1:
                    presenter.setLogger(new HtmlLogger());
                    break;
            }
            TextBoxLog.Text = Log;
        }
    }
}